package it.univaq.mwt.model.entity;

import java.io.Serializable;
import java.util.Set;
import java.util.List;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Column;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

import javax.persistence.SequenceGenerator;

/*
--
-- Struttura della tabella `groups`
--

CREATE TABLE IF NOT EXISTS `groups` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

*/
@Entity
@SequenceGenerator(name="sequence",sequenceName="MASTER.GROUPS_SEQ")
@NamedQueries({ 
	@NamedQuery(name="getIdByIdGroup",query="SELECT g FROM Groups g WHERE g.id=:id_group"),
	@NamedQuery(name="getIdByUser",query="SELECT g FROM Groups g WHERE g.id=:id"),
	@NamedQuery(name="getGroupList", query="SELECT g FROM Groups g")
})
public class Groups implements Serializable {

	@Id
	@GeneratedValue(generator="sequence")
    
	@Column(name="id_group",nullable=false,length=11)
	private int id;
	
	@Column(name="name",nullable=false,length=50)
	private String name;
		
	
	 @OneToMany(mappedBy="groups")
	  private Set<Users> users;	
			
	public Set<Users> getUsers() {
		return users;
	}
	public void setUsers(Set<Users> users) {
		this.users = users;
	}
	
	/*@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER) 
	@JoinTable(name="services_groups",
			joinColumns = {@JoinColumn(name="id_group", referencedColumnName="id_group", nullable=false)},
			inverseJoinColumns={@JoinColumn(name="id_service", referencedColumnName="id_service", nullable=false)})*/
	@ManyToMany(mappedBy="groups",cascade = { CascadeType.MERGE, CascadeType.REFRESH},fetch=FetchType.EAGER)
	private Set<Services> services;
	public Set<Services> getServices() {
		return services;
	}
	public void setServices(Set<Services> services) {
		this.services = services;
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	
}
